package top.crossrun.webview.bridge.core.utils;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Build;
import android.util.Log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

import top.crossrun.webview.bridge.core.pls.WebViewActivityLife;

public class BridgeUtil {
    public static String getFunctionFromReturnUrl(String scheme, String host, String var0) {
        String var1 = null;
        if (var0.startsWith(ble(scheme, host))) {
            var0 = var0.replace(ble(scheme, host), "");
            int i = var0.indexOf("/");
            if (i > 0) {
                var1 = var0.substring(0, i);
            }
        }
        return var1;
    }

    public static String getDataFromReturnUrl(String scheme, String host, String var0) {
        String var3 = null;
        if (var0.startsWith(ble(scheme, host))) {
            var0 = var0.replace(ble(scheme, host), "");
            int i = var0.indexOf("/");
            if (i > 0) {
                var3 = var0.substring(i + 1);
            }
        }
        return var3;
    }

    public static String ble(String scheme, String host) {
        return String.format("%s://%s/", scheme, host);
    }


    public static String parseFunctionName(String var0) {
        return var0.replace("javascript:WebViewJavascriptBridge.", "").replaceAll("\\(.*\\);", "");
    }

    public static boolean isIntentAvailable(Context context, Intent intent) {
        List<ResolveInfo> resolveInfos = intentAvailableList(context, intent);
        if (resolveInfos == null || resolveInfos.size() < 1) {
            return false;
        }
        return true;
    }

    public static List<ResolveInfo> intentAvailableList(Context context, Intent intent) {
        PackageManager pm = context.getPackageManager();
        //queryIntentActivities()方法检索可以针对给定意图执行的所有活动并返回ResolveInfo。
        //若第二个参数设置为PackageManager.MATCH_DEFAULT_ONLY,则只检索显示添加"android.intent.category.DEFAULT"属性的Activity
        List<ResolveInfo> resolveInfos;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            resolveInfos = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
        } else {
            resolveInfos = pm.queryIntentActivities(intent, PackageManager.MATCH_ALL);
        }
        return resolveInfos;
    }


    public static String assetFile2Str(Context var0, String var1) {
        InputStream var2 = null;

        try {
            var2 = var0.getAssets().open(var1);
            BufferedReader var12 = new BufferedReader(new InputStreamReader(var2));
            StringBuilder var3 = new StringBuilder();

            do {
                if ((var1 = var12.readLine()) != null && !var1.matches("^\\s*\\/\\/.*")) {
                    var3.append(var1);
                }
            } while (var1 != null);

            var12.close();
            var2.close();
            String var13 = var3.toString();
            return var13;
        } catch (Exception var10) {
            var10.printStackTrace();
        } finally {
            if (var2 != null) {
                try {
                    var2.close();
                } catch (IOException var9) {
                }
            }

        }

        return null;
    }

    public static String decode2UTF8(String data) throws UnsupportedEncodingException {
        data = data.replaceAll("%(?![0-9a-fA-F]{2})", "%25");
        data = data.replaceAll("\\+", "%2B");
        return URLDecoder.decode(data, "utf-8");
    }

    public static String decode(String data) {
        data = data.replaceAll("%(?![0-9a-fA-F]{2})", "%25");
        data = data.replaceAll("\\+", "%2B");
        return URLDecoder.decode(data);
    }

    /**
     * 判断是否实现类ActivityLife接口
     *
     * @param s
     * @return
     */
    public static boolean isAssginFromActivityLife(Class s) {
        return WebViewActivityLife.class.isAssignableFrom(s);
    }
}
